package com.zhudao.crawler.service;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AnjukeService {
    private static final Logger logger = LoggerFactory.getLogger(AnjukeService.class);


    @Autowired
    @Qualifier("primaryJdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    @Transactional(rollbackFor = Exception.class)
    public void runAnjukeCommuity() throws IOException, InterruptedException {
        List<Map<String, Object>> list = jdbcTemplate.queryForList("SELECT * from city_business");
        for (Map<String, Object> map : list) {
            String en_name = (String) map.get("en_name");
            String en_name_a = en_name;
            String name = (String) map.get("name");
            for (int i = 1; i < 501; i++) {
                URL url = new URL("https://shanghai.anjuke.com/community/" + en_name_a + "/p" + i + "/");
                Document document = Jsoup.parse(url, 10000);
                Elements elements = document.select("div.li-info > h3 > a");
                if (elements == null || elements.first() == null || elements.first().text() == null || elements.first().text().equals("汤臣一品")) {
                    logger.info("参数name:{},{}页,跳过", en_name, i);
                    break;
                }
                logger.info("参数name:{},{}页,存储中", en_name, i);
                Elements elements1 = document.select("div.li-info > address");
                Elements elements2 = document.select("div.li-info > p.bot-tag > a:nth-child(2)");
                Elements elements3 = document.select("div.li-info > p.date");
                Thread.sleep(300);
                Integer id = (Integer) map.get("id");
                for (int k = 0; k < elements.size(); k++) {
                    String community_name = elements.get(k).text();
                    String source_map_link = elements2.get(k).attr("href");
                    String completion_date = elements3.get(k).text();
                    String source_community_address = elements1.get(k).text();
                    String town_name = source_community_address.substring(source_community_address.indexOf("-") + 1, source_community_address.lastIndexOf("］"));
                    String commid = source_map_link.substring(source_map_link.indexOf("commid=") + 7);
                    String district_name = source_community_address.substring(source_community_address.indexOf("［") + 1, source_community_address.indexOf("-"));
                    String community_address = source_community_address.substring(source_community_address.indexOf("］") + 1);
                    String lat = source_map_link.substring(source_map_link.indexOf("l1=") + 3, source_map_link.indexOf("&l2"));
                    String lng = source_map_link.substring(source_map_link.indexOf("l2=") + 3, source_map_link.indexOf("&l3"));
                    Map<String, Object> map1 = jdbcTemplate.queryForMap("SELECT COUNT(1) as count from city_community where  source_commnity_id=?", commid);
                    Integer count = Integer.valueOf(map1.get("count").toString());
                    if (count == 0) {
                        jdbcTemplate.update("INSERT into city_community" +
                                "        (id,source_commnity_id,createTime,community_name,source_map_link,completion_date,source_community_address,town_name,business_name,business_en_name,business_id,district_name,community_address,lat,lng)" +
                                " VALUES (NULL,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", commid, new Date(), community_name, source_map_link, completion_date, source_community_address, town_name, name, en_name, id, district_name, community_address, lat, lng);
                    }
                }
            }
        }


//                URL url1 = new URL(" https://shanghai.anjuke.com/community/view/" + Id);
//                Document document3 = Jsoup.parse(url1, 10000);
//                String text4 = document3.select("#basic-infos-box > dl > dd:nth-child(2)").text();
//                System.out.println("text4:" + text4);
//                String text5 = document3.select("#basic-infos-box > dl > dd:nth-child(6)").text();
//                System.out.println("text5:" + text5);
//                String text6 = document3.select("#basic-infos-box > dl > dd:nth-child(10)").text();
//                System.out.println("text6:" + text6);
//                String text7 = document3.select("#basic-infos-box > dl > dd:nth-child(8)").text();
//                System.out.println("text7:" + text7);
//                String text8 = document3.select("#basic-infos-box > dl > dd:nth-child(14)").text();
//                System.out.println("text8:" + text8);
//                String text9 = document3.select("#basic-infos-box > dl > dd:nth-child(16)").text();
//                System.out.println("text9:" + text9);
//                String text10 = document3.select("#basic-infos-box > dl > dd:nth-child(18)").text();
//                System.out.println("text10:" + text10);
//                String text11 = document3.select("#basic-infos-box > dl > dd:nth-child(20)").text();
//                System.out.println("text11:" + text11);
//                String text12 = document3.select("#basic-infos-box > dl > dd:nth-child(12)").text();
//                System.out.println("text12:" + text12);
//
//                URL url2 = new URL(" https://shanghai.anjuke.com/round/" + Id);
//                Document document4 = Jsoup.parse(url2, 10000);
//                String text13 = document3.select("#content > div.mating-contents > div.mating-main > div.periphery-mod > div > div > div > p").text();
//                String text14 = document3.select(".metro-list").text();


//            }
    }

}
